package com.pswv2.backstage.more.requirements.user.action;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.pswv2.backstage.more.requirements.user.service.UserRequirementsService;
import com.pswv2.backstage.user.service.UserService;
import com.pswv2.bub.bsc.dao.po.AccountDetails;
import com.pswv2.bub.bsc.dao.po.AnswerRequirements;
import com.pswv2.bub.bsc.dao.po.Authentication;
import com.pswv2.bub.bsc.dao.po.DataManagement;
import com.pswv2.bub.bsc.dao.po.MyWallet;
import com.pswv2.bub.bsc.dao.po.Territory;
import com.pswv2.bub.bsc.dao.po.Territory2;
import com.pswv2.bub.bsc.dao.po.User;
import com.pswv2.bub.bsc.dao.po.UserRequirements;
import com.pswv2.bub.bsc.dao.po.UserTerritory;
import com.pswv2.bub.bsc.dao.po.UserUserRequirements;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Paging;
import com.pswv2.util.Sessions;
/**
 * 用户回发布问题
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "userRequirementsAction", produces = { "text/json;charset=UTF-8" })
public class UserRequirementsAction {
	private static Class<?> pocDM = DataManagement.class;
	private static Class<?> pocUUR = UserUserRequirements.class;
	private static Class<?> pocMW = MyWallet.class;
	private static Class<?> pocUT = UserTerritory.class;
	private static Class<?> pocA = AnswerRequirements.class;
	private static Class<?> pocU = User.class;
	private static Class<?> poc = UserRequirements.class;
	private static Class<?> pocT = Territory.class;
	private static Class<?> pocT2 = Territory2.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private UserRequirementsService userRequirementsService;
	
	/**
	 * 发布问题支付积分
	 * @return
	 */
	@ResponseBody
	@RequestMapping("payPionts")
	public String payPionts(int userId,int points) {
		User user=extraSpringHibernateTemplate.findFirstOneByPropEq(pocU, "userId", userId);
		double points1=user.getPoints();
		System.out.print("在一起啊");
		if(points1-points>=0){
			user.setPoints(points1-points);
			extraSpringHibernateTemplate.getHibernateTemplate().update(user);
			AccountDetails ad=new AccountDetails();
			ad.setPoints(points);
			ad.setUserId(userId);
			ad.setShepoints(points1-points);
			ad.setType(AccountDetails.pionts);
			ad.setMatter("提问消费");
			ad.setProceedsPaid(1);
			Long sar=new Date().getTime();
			ad.setTime(sar);
			ad.setType(2);
			extraSpringHibernateTemplate.getHibernateTemplate().save(ad);
			List<DataManagement> lsdm=(List<DataManagement>) extraSpringHibernateTemplate.findAll(pocDM);
			DataManagement dm=lsdm.get(0);
			double all=dm.getAllFionts();
			double alls=all+points;
			dm.setAllFionts(alls);
			extraSpringHibernateTemplate.getHibernateTemplate().update(dm);	
			return CommonResp.buildSuccessResp().toJsonString();	
		}else{
			return CommonResp.buildErrorResp(BackendErrorCode.not_sufficient_funds).toJsonString();
		}
		
	}
	/**
	 * 发布问题的积分
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appEditPoints")
	public String appEditPoints(int userRequirementsId,double totalPoints) {
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userRequirementsId", userRequirementsId);
		ur.setTotalPoints(totalPoints);
		ur.setPoints(totalPoints);
		extraSpringHibernateTemplate.getHibernateTemplate().update(ur);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 查询是否回答
	 * @return
	 */
	@ResponseBody
	@RequestMapping("existByUserId")
	public String existByUserId(int userRequirementsId,int userId) {
		List ls=extraSpringHibernateTemplate.getHibernateTemplate().find("SELECT ar.kWord,ar.content,u.name From AnswerRequirements ar, User u where ar.useuId=u.userId  and ar.userRequirementsId=? and ar.userId=? ",userRequirementsId,userId);
		if(ls.size()==0){
			UserRequirements ur=userRequirementsService.queryById(userRequirementsId);
			return CommonResp.buildSuccessResp(ur).toJsonString();
		}else{
			return CommonResp.buildErrorResp(BackendErrorCode.i_ve_answered).toJsonString();
		}
	}
	/**
	 * 结案
	 * @return
	 */
	@ResponseBody
	@RequestMapping("closingACase")
	public String closingACase(int userRequirementsId,int userId,double totalAmount) {
		List<DataManagement> lsdm=(List<DataManagement>) extraSpringHibernateTemplate.findAll(pocDM);
		DataManagement dm=lsdm.get(0);
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userRequirementsId", userRequirementsId);
		ur.setStatus(ur.status_closing_a_case);
		Long s=new Date().getTime();
		long releaseTime=ur.getReleaseTime();
		double moneyUR=ur.getTotalPrice();
		extraSpringHibernateTemplate.getHibernateTemplate().update(ur);
		List<AnswerRequirements> lsa=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userRequirementsId=? and statusSee=?", ur.getUserRequirementsId(),2);
		double totalAmountDM=0;
		double totalAmountX=0;
		for(AnswerRequirements ar:lsa){
			int aUserId=ar.getUserId();
			double degreeOfSatisfaction=ar.getDegreeOfSatisfaction();
			long answerTime=ar.getAnswerTime();
			long sd=answerTime-releaseTime;
	        long dd=sd/(1000 * 60 * 60 *24);
			double remuneration=ar.getRemunerationPrice();
			MyWallet mwa=extraSpringHibernateTemplate.findFirstOneByPropEq(pocMW, "userId", aUserId);
			if(mwa==null){
				MyWallet mw1=new MyWallet();
				mw1.setMoney((remuneration*(degreeOfSatisfaction/10)/(dd+1))*8/10);
				mw1.setUserId(aUserId);
				extraSpringHibernateTemplate.getHibernateTemplate().save(mw1);
			}else{
				double m=mwa.getMoney();
				mwa.setMoney(m+((remuneration*(degreeOfSatisfaction/10)/(dd+1))*8/10));
				extraSpringHibernateTemplate.getHibernateTemplate().update(mwa);
			}
			totalAmountX=totalAmountX+(remuneration*(degreeOfSatisfaction/10)/(dd+1));
			totalAmountDM=totalAmountDM+((remuneration*(degreeOfSatisfaction/10)/(dd+1))*2/10);
			ar.setStatusPid(1);
			extraSpringHibernateTemplate.getHibernateTemplate().update(ar);
			AccountDetails adar=new AccountDetails();
			adar.setUserId(aUserId);
			adar.setMatter("回复报酬");
			adar.setAmount((remuneration*(degreeOfSatisfaction/10)/(dd+1))*8/10);
			adar.setProceedsPaid(0);
			Long sar=new Date().getTime();
			adar.setTime(sar);
			adar.setType(1);
			extraSpringHibernateTemplate.getHibernateTemplate().save(adar);
			extraSpringHibernateTemplate.getHibernateTemplate().update(ar);
		}
		totalAmountDM=dm.getAllFund()+totalAmountDM-moneyUR;
		dm.setAllFund(totalAmountDM);
		extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
		MyWallet mw=extraSpringHibernateTemplate.findFirstOneByPropEq(pocMW, "userId", userId);
		double money=mw.getMoney();
		mw.setMoney(money-totalAmountX+moneyUR);
		extraSpringHibernateTemplate.getHibernateTemplate().update(mw);
		AccountDetails adur=new AccountDetails();
		adur.setUserId(userId);
		adur.setMatter("需求支付");
		adur.setAmount(totalAmountX);
		adur.setProceedsPaid(1);
		adur.setTime(s);
		adur.setType(1);
		extraSpringHibernateTemplate.getHibernateTemplate().save(adur);
		AccountDetails adur1=new AccountDetails();
		adur1.setUserId(userId);
		adur1.setMatter("返回金额");
		adur1.setAmount(ur.getTotalPrice()-totalAmountX);
		adur1.setProceedsPaid(1);
		adur1.setTime(s);
		adur1.setType(1);
		extraSpringHibernateTemplate.getHibernateTemplate().save(adur1);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 积分结案
	 * @return
	 */
	@ResponseBody
	@RequestMapping("closingACasePionts")
	public String closingACasePionts(int userRequirementsId,int userId,double totalAmount) {
		List<DataManagement> lsdm=(List<DataManagement>) extraSpringHibernateTemplate.findAll(pocDM);
		DataManagement dm=lsdm.get(0);
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userRequirementsId", userRequirementsId);
		ur.setStatus(ur.status_closing_a_case);
		Long s=new Date().getTime();
		long releaseTime=ur.getReleaseTime();
		double moneyUR=ur.getTotalPoints();
		extraSpringHibernateTemplate.getHibernateTemplate().update(ur);
		List<AnswerRequirements> lsa=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userRequirementsId=? and statusSee=?", ur.getUserRequirementsId(),2);
		double totalAmountDM=0;
		double totalAmountX=0;
		for(AnswerRequirements ar:lsa){
			int aUserId=ar.getUserId();
			double degreeOfSatisfaction=ar.getDegreeOfSatisfaction();
			long answerTime=ar.getAnswerTime();
			long sd=answerTime-releaseTime;
			long dd=sd/(1000 * 60 * 60 *24);
			double remuneration=ar.getRemunerationPoints();
			User mwa=extraSpringHibernateTemplate.findFirstOneByPropEq(pocU, "userId", aUserId);
			double m=mwa.getPoints();
			mwa.setPoints(m+((remuneration*(degreeOfSatisfaction/10)/(dd+1))*8/10));
			extraSpringHibernateTemplate.getHibernateTemplate().update(mwa);
			totalAmountX=totalAmountX+(remuneration*(degreeOfSatisfaction/10)/(dd+1));
			totalAmountDM=totalAmountDM+((remuneration*(degreeOfSatisfaction/10)/(dd+1))*2/10);
			ar.setStatusPid(1);
//			extraSpringHibernateTemplate.getHibernateTemplate().update(ar);
			AccountDetails adar=new AccountDetails();
			adar.setUserId(aUserId);
			adar.setMatter("回复报酬");
			adar.setPoints((remuneration*(degreeOfSatisfaction/10)/(dd+1))*8/10);
			adar.setProceedsPaid(0);
			adar.setType(2);
			Long sar=new Date().getTime();
			adar.setTime(sar);
			extraSpringHibernateTemplate.getHibernateTemplate().save(adar);
			extraSpringHibernateTemplate.getHibernateTemplate().update(ar);
		}
		totalAmountDM=dm.getAllFionts()+totalAmountX;
		dm.setAllFionts(totalAmountDM);
		extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
		User mw=extraSpringHibernateTemplate.findFirstOneByPropEq(pocU, "userId", userId);
		double points=mw.getPoints();
		mw.setPoints(points-totalAmountX+moneyUR);
		extraSpringHibernateTemplate.getHibernateTemplate().update(mw);
		/*AccountDetails adur=new AccountDetails();
		adur.setUserId(userId);
		adur.setMatter("需求支付");
		adur.setPoints(totalAmountX);
		adur.setProceedsPaid(1);
		adur.setTime(s);
		adur.setType(2);
		extraSpringHibernateTemplate.getHibernateTemplate().save(adur);*/
		AccountDetails adur1=new AccountDetails();
		adur1.setUserId(userId);
		adur1.setMatter("返回积分");
		adur1.setPoints(ur.getTotalPoints()-totalAmountX);
		adur1.setProceedsPaid(1);
		adur1.setTime(s);
		adur1.setType(2);
		extraSpringHibernateTemplate.getHibernateTemplate().save(adur1);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 计算总金额
	 * @return
	 */
	@ResponseBody
	@RequestMapping("calculationTotalAmount")
	public String calculationTotalAmount(int userRequirementsId) {
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userRequirementsId", userRequirementsId);
		long releaseTime=ur.getReleaseTime();
		List<AnswerRequirements> lsar=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userRequirementsId=? and statusSee=?", userRequirementsId,AnswerRequirements.discuss);
		if(lsar.size()==0){
			return CommonResp.buildSuccessResp(0).toJsonString();
		}else{
			double totalAmount=0;
			for(AnswerRequirements ar:lsar){
				double degreeOfSatisfaction=ar.getDegreeOfSatisfaction();
				long answerTime=ar.getAnswerTime();
				long sd=answerTime-releaseTime;
		        long dd=sd/(1000 * 60 * 60 *24);
				double remuneration=ar.getRemunerationPrice();
				totalAmount=totalAmount+(remuneration*(degreeOfSatisfaction/10)/(dd+1));
			}
			DecimalFormat df = new DecimalFormat("#0.00");
			
			return CommonResp.buildSuccessResp(Double.valueOf(df.format(totalAmount))).toJsonString();
		}
	}
	/**
	 * 计算总积分
	 * @return
	 */
	@ResponseBody
	@RequestMapping("calculationTotalPoints")
	public String calculationTotalPoints(int userRequirementsId) {
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userRequirementsId", userRequirementsId);
		long releaseTime=ur.getReleaseTime();
		List<AnswerRequirements> lsar=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userRequirementsId=? and statusSee=?", userRequirementsId,AnswerRequirements.discuss);
		if(lsar.size()==0){
			return CommonResp.buildSuccessResp(0).toJsonString();
		}else{
			int totalAmount=0;
			for(AnswerRequirements ar:lsar){
				double degreeOfSatisfaction=ar.getDegreeOfSatisfaction();
				long answerTime=ar.getAnswerTime();
				long sd=answerTime-releaseTime;
				long dd=sd/(1000 * 60 * 60 *24);
				double remuneration=ar.getRemunerationPoints();
				totalAmount=(int) (totalAmount+(remuneration*(degreeOfSatisfaction/10)/(dd+1)));
			}
			DecimalFormat df = new DecimalFormat("#0.00");
			
			return CommonResp.buildSuccessResp(Double.valueOf(df.format(totalAmount))).toJsonString();
		}
	}
	/**
	 * 条件查询
	 */
	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String kwords, HttpSession session) throws ParseException {
		
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(kwords)) {
			criterions.add(Restrictions.ilike("kwords", "%" + kwords + "%"));
		}
		//criterions.add(Restrictions.eq("status", ShareKWord.status_pass));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("userRequirementsId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	/**
	 * pc 查询所有用户
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryAll")
	public String queryAll(int pageIndex, int pageSize) {
		pageIndex=pageIndex*pageSize;
		//List<UserRequirements> ls=(List<UserRequirements>) extraSpringHibernateTemplate.findAll(poc);
		Paging<?> pag=extraSpringHibernateTemplate.findPage(poc, Order.desc("userRequirementsId"), pageIndex, pageSize);
		/*List ls1=new ArrayList<>();
		for(UserRequirements EntityUR:ls){
			Map map=new HashMap<String,Object>();
			int userId=EntityUR.getUserId();
			User us=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", userId);
			map.put("releaseName", us.getName());
			map.put("ur", EntityUR);
			ls1.add(map);
		}*/
		return CommonResp.buildSuccessResp(pag).toJsonString();
	}
	/**
	 * app 通过id查询带用户名的
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryById")
	public String queryById(int userRequirementsId) {
		UserRequirements EntityUR=userRequirementsService.queryById(userRequirementsId);
		Map map=new HashMap<String,Object>();
		int userId=EntityUR.getUserId();
		User us=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", userId);
		map.put("releaseName", us.getName());
		map.put("ur", EntityUR);
		return CommonResp.buildSuccessResp(map).toJsonString();
	}
	/**
	 * app 通过userRequirementsId查询带
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByUserRequirementsId")
	public String queryByUserRequirementsId(int userRequirementsId) {
		UserRequirements EntityUR=userRequirementsService.queryById(userRequirementsId);
		return CommonResp.buildSuccessResp(EntityUR).toJsonString();
	}
	/**
	 * app 求助同一领域的他人
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("sendOtherRequirements")
	public String sendOtherRequirements(int userRequirementsId,int territoryId,int userId) {
		/*List<UserTerritory> ut=new ArrayList<UserTerritory>();
		ut=extraSpringHibernateTemplate.findAllByPropEq(pocUT, "territoryId", territoryId);
		for(UserTerritory EntityUT:ut){
			if(EntityUT.getUserId()!=userId){
				int utId=EntityUT.getUserId();
				UserRequirements EntityUR=userRequirementsService.queryById(userRequirementsId);
				User uesrs=(User) extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", utId);
				String adds=uesrs.getWorkAddress();
				String adr=EntityUR.getPublishingAddress();
				if(adds.equals(adr)){
					UserUserRequirements uur=new UserUserRequirements();
					uur.setUserRequirementsId(userRequirementsId);
					uur.setStatus(0);
					uur.setUserId(EntityUT.getUserId());
					List ls=extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", EntityUT.getUserId(),userRequirementsId);
					if(ls.size()==0){
						extraSpringHibernateTemplate.getHibernateTemplate().save(uur);
					}
				}
			}
		}*/
		//优化代码
		List<Integer> ls=(List<Integer>) extraSpringHibernateTemplate.
				getHibernateTemplate().
				find("SELECT u.userId FROM UserTerritory ut, "
						+ "User u,UserRequirements ur WHERE ut.territoryId=?"
						+ " AND u.userId=ut.userId AND "
						+ "ur.userRequirementsId=? AND"
						+ " u.workAddress=ur.publishingAddress GROUP BY u.userId",territoryId,userRequirementsId);
		for(int i=0;i<ls.size();i++){
			if(ls.get(i)!=userId){
				int st=ls.get(i);
				UserUserRequirements uur=new UserUserRequirements();
				uur.setUserRequirementsId(userRequirementsId);
				uur.setStatus(0);
				uur.setUserId(st);
				List ls1=extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", st,userRequirementsId);
				if(ls1.size()==0){
					extraSpringHibernateTemplate.getHibernateTemplate().save(uur);
				}
			}
			
		}
		/*ut=(List<UserTerritory>) extraSpringHibernateTemplate.getHibernateTemplate().find("From UserTerritory where userRequirementsId=? and statusSee=?", userRequirementsId,AnswerRequirements.discuss);
		for(UserTerritory EntityUT:ut){
			if(EntityUT.getUserId()!=userId){
				UserUserRequirements uur=new UserUserRequirements();
				uur.setUserRequirementsId(userRequirementsId);
				uur.setStatus(0);
				uur.setUserId(EntityUT.getUserId());
				List ls=extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and territoryId=?", territoryId,userRequirementsId);
				if(ls.size()==0){
					extraSpringHibernateTemplate.getHibernateTemplate().save(uur);
				}
			}
		}*/
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app 查询是否有被求助
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("sendOtherRequirementss")
	public String sendOtherRequirementss(int userRequirementsId,int territoryId,int userId) {
		List<UserTerritory> ut=new ArrayList<UserTerritory>();
		ut=extraSpringHibernateTemplate.findAllByPropEq(pocUT, "territoryId", territoryId);
		for(UserTerritory EntityUT:ut){
			if(EntityUT.getUserId()!=userId){
				UserUserRequirements uur=new UserUserRequirements();
				uur.setUserRequirementsId(userRequirementsId);
				uur.setStatus(0);
				uur.setUserId(EntityUT.getUserId());
				List ls=extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", EntityUT.getUserId(),userRequirementsId);
				if(ls.size()==0){
					extraSpringHibernateTemplate.getHibernateTemplate().save(uur);
				}
			}
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app 查询是否有被回答的没得
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("sendOtherRequirementsss")
	public String sendOtherRequirementsss(int userRequirementsId,int territoryId,int userId) {
		List<UserTerritory> ut=new ArrayList<UserTerritory>();
		ut=extraSpringHibernateTemplate.findAllByPropEq(pocUT, "territoryId", territoryId);
		for(UserTerritory EntityUT:ut){
			if(EntityUT.getUserId()!=userId){
				UserUserRequirements uur=new UserUserRequirements();
				uur.setUserRequirementsId(userRequirementsId);
				uur.setStatus(0);
				uur.setUserId(EntityUT.getUserId());
				List ls=extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", EntityUT.getUserId(),userRequirementsId);
				if(ls.size()==0){
					extraSpringHibernateTemplate.getHibernateTemplate().save(uur);
				}
			}
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app userId查询他人的需求
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryOtherRequirements")
	public String queryOtherRequirements(int userId) {
		List arrayList=new ArrayList<>();
		List ls=extraSpringHibernateTemplate.getHibernateTemplate().find("SELECT ur.status,ur.userRequirementsId,ur.releaseTime,ur.kWord,ur.content,u.name,uur.status FROM UserUserRequirements uur,UserRequirements ur , User u where  uur.userRequirementsId=ur.userRequirementsId and ur.userId=u.userId and uur.userId=?",userId);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 我擅长的领域是否有人提问
	 * app userId查询他人的需求
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryOtherRequirementsf")
	public String queryOtherRequirementsf(int userId) {
		List arrayList=new ArrayList<>();
		List ls=extraSpringHibernateTemplate.getHibernateTemplate().find
				("SELECT ur.status,ur.userRequirementsId,ur.releaseTime,"
						+ "ur.kWord,ur.content,u.name,uur.status FROM "
						+ "UserUserRequirements uur,UserRequirements ur , User u "
						+ "where  uur.userRequirementsId=ur.userRequirementsId "
						+ "and ur.userId=u.userId and uur.status=0 and uur.userId=?",userId);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 已查看过擅长领域的问题
	 * app userId查询他人的需求
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("seeOtherRequirementsf")
	public String seeOtherRequirementsf(int userId) {
		List<UserUserRequirements> ls=extraSpringHibernateTemplate.findAllByPropEq(UserUserRequirements.class, "userId", userId);
		for(UserUserRequirements us:ls){
			us.setStatus(2);
			extraSpringHibernateTemplate.getHibernateTemplate().update(us);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app通过userId查询发布了多少需求
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByUserIdRequirements")
	public String queryByIdRequirements(int userId) {
		List<UserRequirements> ls=userRequirementsService.queryByUserIdRequirements(userId);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * app添加中等模式
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appMediumAdd")
	public String appMediumAdd(int userId,String kWord,String content,int level,
			String publishingAddress,int territoryId,String method, String term1, String term2) {
		UserRequirements userRequirements=new UserRequirements();
		Long s=new Date().getTime();
		userRequirements.setReleaseTime(s);
		userRequirements.setUserId(userId);
		userRequirements.setStatus(UserRequirements.status_wait);
		userRequirements.setkWord(kWord);
		userRequirements.setMethod(method);
		userRequirements.setTerm1(term1);
		userRequirements.setTerm2(term2);
		userRequirements.setContent(content);
		userRequirements.setLevel(level);
		userRequirements.setPublishingAddress(publishingAddress);
		userRequirements.setTerritoryId(territoryId);
		extraSpringHibernateTemplate.getHibernateTemplate().save(userRequirements);
		return CommonResp.buildSuccessResp(userRequirements).toJsonString();
	}
	/**
	 * app添加简单模式
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appAdd")
	public String appAdd(int userId,String kWord,String content,int level,
			Integer fulfilTime,String publishingAddress,int territoryId,String othert) {
		
		UserRequirements userRequirements=new UserRequirements();
		Long s=new Date().getTime();
		userRequirements.setReleaseTime(s);
		userRequirements.setUserId(userId);
		userRequirements.setStatus(UserRequirements.status_wait);
		userRequirements.setkWord(kWord);
		userRequirements.setContent(content);
		userRequirements.setLevel(level);
		userRequirements.setFulfilTime(fulfilTime);
		userRequirements.setPublishingAddress(publishingAddress);
		userRequirements.setTerritoryId(territoryId);
		Territory ty=(Territory) extraSpringHibernateTemplate.findFirstOneByPropEq(pocT, "territoryId", territoryId);
		if(ty.getName().equals("其它")){
			List<Territory2> ls=extraSpringHibernateTemplate.findAllByPropEq(pocT2, "name", othert);
			if(ls.size()==0){
				Territory2 T2=new Territory2();
				T2.setTerritoryId(territoryId);
				T2.setName(othert);
				extraSpringHibernateTemplate.getHibernateTemplate().save(T2);
			}
			Territory2 T2s=(Territory2) extraSpringHibernateTemplate.findFirstOneByPropEq(pocT2, "name", othert);
			userRequirements.setTerritory2Id(T2s.getTerritory2Id());
		}
		extraSpringHibernateTemplate.getHibernateTemplate().save(userRequirements);
		return CommonResp.buildSuccessResp(userRequirements).toJsonString();
	}
	/**
	 * app修改价钱
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appEditPrice")
	public String appEditPrice(double totalDprice,int userRequirementsId) {
		UserRequirements userRequirements=userRequirementsService.queryById(userRequirementsId);
		/*List<DataManagement> ls1=(List<DataManagement>) extraSpringHibernateTemplate.findAll(DataManagement.class);
		DataManagement dm=ls1.get(0);*/
//		double pres=Double.parseDouble(totalDprice);
//		userRequirements.setPrice(totalDprice/dm.getRequirementReleaseAmount());
		userRequirements.setPrice(totalDprice);
		userRequirements.setTotalPrice(totalDprice);
		extraSpringHibernateTemplate.getHibernateTemplate().update(userRequirements);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app支付成功
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appPid")
	public String appPid(int userRequirementsId) {
		UserRequirements userRequirements=userRequirementsService.queryById(userRequirementsId);
		userRequirements.setPidStatus(userRequirements.paid);
		userRequirements.setStatus(UserRequirements.status_pass);
		extraSpringHibernateTemplate.getHibernateTemplate().update(userRequirements);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 	pc后台选中通过
	 */
	@ResponseBody
	@RequestMapping(value={"pitchOnPass"})
	public String pitchOnPass(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			UserRequirements entitu = userRequirementsService.queryById(a);
			entitu.setStatus(entitu.status_pass);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entitu);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 	pc后台选中驳回
	 */
	@ResponseBody
	@RequestMapping(value={"pitchOnReject"})
	public String pitchOnReject(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			UserRequirements entitu = userRequirementsService.queryById(a);
			entitu.setStatus(entitu.status_reject);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entitu);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 	pc后台删除选中的
	 */
	@ResponseBody
	@RequestMapping(value={"pitchOnDelete"})
	public String pitchOnDelete(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			UserRequirements entitu = userRequirementsService.queryById(a);
			extraSpringHibernateTemplate.getHibernateTemplate().delete(entitu);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	/**
	 * 单一删除
	 */
	@ResponseBody
	@RequestMapping(value = { "remove" })
	public String remove(int id) {
		UserRequirements entitu = userRequirementsService.queryById(id);
		if (entitu == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(entitu);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 单一审核通过
	 */
	@ResponseBody
	@RequestMapping(value = "pass")
	public String pass(Integer userRequirementsId) {
			UserRequirements entitu=userRequirementsService.queryById(userRequirementsId);
			entitu.setStatus(Authentication.pass);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entitu);
			return CommonResp.buildSuccessResp().toJsonString();
	     }
	/**
	 * 单一审核驳回
	 */
	@ResponseBody
	@RequestMapping(value = "reject")
	public String reject(Integer userRequirementsId) {
		UserRequirements entitu=userRequirementsService.queryById(userRequirementsId);
		entitu.setStatus(Authentication.reject);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entitu);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
